package com.jsj.service;

import com.jsj.dao.ProblemDao;
import com.jsj.dao.TopicDao;
import com.jsj.domain.Problem;
import com.jsj.domain.TopClass;
import com.jsj.domain.Topic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TopicService {
    private TopicDao topicDao;
    private ProblemDao problemDao;

    @Autowired
    public void setProblemDao(ProblemDao problemDao) {
        this.problemDao = problemDao;
    }

    @Autowired
    public void setTopicDao(TopicDao topicDao) {
        this.topicDao = topicDao;
    }

    public List<Topic> queryByCreator(String creator) {
        return topicDao.queryByCreator(creator);
    }
    public List<Topic> queryByTitle(String title) {
        return topicDao.queryByTitle(title);
    }
    public List<Topic> queryByClass(int classId) {
        return topicDao.queryByClass(classId);
    }

    public List<Topic> queryByTeacher(int teacherId) {
        return topicDao.queryByTeacher(teacherId);
    }

    public List<Topic> queryByClassNow(int classId) {
        return topicDao.queryByClassNow(classId);
    }

    public List<Topic> queryByClassBefore(int classId) {
        return topicDao.queryByClassBefore(classId);
    }

    public List<Topic> queryByClassAfter(int classId) {
        return topicDao.queryByClassAfter(classId);
    }

    public Topic queryById(int id) {
        Topic topic = topicDao.queryById(id);
        if (topic != null) {
            topic.setProblems(problemDao.queryByTopic(id));
        }
        return topic;
    }

    @Transactional
    public Integer insert(Topic topic) {
        if (topicDao.insert(topic) == 1) {
            if (topic.getProblems() != null) {
                int i = 1;
                for (Problem problem : topic.getProblems()) {
                    topicDao.insertTopicProblem(topic.getId(), problem.getId(), i);
                    i++;
                }
            }
            return 1;
        }
        return 0;
    }

    public Integer delete(int id) {
        return topicDao.delete(id);
    }

    public Integer allocateForClass(int classId, int topicId) {
        return topicDao.allocateForClass(classId, topicId);
    }
    public Integer allocateForClass1(TopClass topClass) {
        return topicDao.allocateForClass1(topClass);
    }

    public Integer unAssignForClass(int classId, int topicId) {
        return topicDao.unAssignForClass(classId, topicId);
    }
    public Integer update(Topic topic) {
        return topicDao.update(topic);
    }
    public Integer queryClassByTopic(int topicId) {
        return topicDao.queryClassByTopic(topicId);
    }
    public Integer queryByFileName(String fileName) {
        return topicDao.queryByFileName(fileName);
    }
    public Integer checkmyself(Topic topic) {
        return topicDao.checkmyself(topic);
    }
    public List<String> selectPushList(String classId){return topicDao.SelectPushList(classId);}
}
